﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Acms.Exceptions;
using System.Linq;

namespace Registration
{

    public partial class WorldClassDataContext
    {
        public NewsletterSignup NewsletterSignups_GetByEmailAddress(string emailAddress)
        {
            return
                (
                    from c in 
                    this.NewsletterSignups 
                    where c.EmailAddress == emailAddress 
                    select c
                ).SingleOrDefault();
        }

        partial void InsertNewsletterSignup(NewsletterSignup instance)
        {
            if(NewsletterSignups_GetByEmailAddress(instance.EmailAddress) != null)
            {
                throw new ValidationException("Email Address Already Registered");
            }
            ExecuteDynamicInsert(instance);
        }

        public WorldClassSignup WorldClassSignup_GetByEmailAddress(string emailAddress)
        {
            return (
                    from tmp in this.WorldClassSignups
                    where tmp.EmailAddress == emailAddress
                    select tmp
                    ).SingleOrDefault();
        }

        partial void UpdateNewsletterSignup(NewsletterSignup instance)
        {
            NewsletterSignup tmp = NewsletterSignups_GetByEmailAddress(instance.EmailAddress);
            if(tmp != null && tmp != instance)
            {
                throw new ValidationException("Email Address Already Registered");
            }
            ExecuteDynamicUpdate(instance);
        }


        partial void InsertWorldClassSignup(WorldClassSignup instance)
        {
            if (WorldClassSignup_GetByEmailAddress(instance.EmailAddress) != null)
            {
                throw new ValidationException("Email Address Already Registered");
            }
            ExecuteDynamicInsert(instance);
        }

        partial void UpdateWorldClassSignup(WorldClassSignup instance)
        {
            if (WorldClassSignup_GetByEmailAddress(instance.EmailAddress) != null && WorldClassSignup_GetByEmailAddress(instance.EmailAddress) != instance)
            {
                throw new ValidationException("Email Address Already Registered");
            }
            ExecuteDynamicUpdate(instance);
        }

        public List<NewsletterSignup> NewsletterSignups_GetAll()
        {
            return this.NewsletterSignups.ToList();
        }

        public List<NewsletterSignup> NewsletterSignups_GetRange(DateTime? startDate, DateTime? endDate)
        {
            List<NewsletterSignup> list = null;
            if(startDate != null && endDate != null)
            {
                list = (from tmp in this.NewsletterSignups where tmp.Created >= startDate.Value && tmp.Created <= endDate.Value select tmp).ToList();
            }
            else if (startDate != null)
            {
                list = (from tmp in this.NewsletterSignups where tmp.Created >= startDate.Value select tmp).ToList();
            }
            else if (endDate != null)
            {
                list = (from tmp in this.NewsletterSignups where tmp.Created <= endDate.Value select tmp).ToList();
            }
            else
            {
                list = (from tmp in this.NewsletterSignups select tmp).ToList();
            }
            return list;
        }
    }
}
